// thread.php 相关工具方法

import {UrlUtils} from "@/assets/js/UrlUtils";
import {routeNames} from "@/router";
import {colListParam, ColSearchParam, FavorParam, ForumListParam, ForumRecommendParam, ForumSearchParam, UserSearchParam} from "@ginstone/nga-api/dist/src/params/ThreadParams";
import {NgaDomain} from "@ginstone/nga-api/dist/src/enums/NgaDomain";
// api
const api = "https://bbs.nga.cn/thread.php"

export class ThreadUtils {
    /**
     * 根据路由生成 thread.php 请求需要的参数
     * @param route 路由
     * @returns {FavorParam|ColSearchParam|UserSearchParam|ForumSearchParam|colListParam|ForumListParam|ForumRecommendParam}
     */
    static buildParamsFromRoute(route) {
        return this.buildParams(route.name, Object.assign({}, route.params, route.query));
    }

    /**
     * 从根据参数和路由名称，生成 API需要的参数对象
     * @param name 路由名称
     * @param params 参数
     * @returns {FavorParam|ColSearchParam|UserSearchParam|ForumSearchParam|colListParam|ForumListParam|ForumRecommendParam}
     */
    static buildParams(name, params,) {
        const threadRouteName = routeNames.thread
        const userRouteName = routeNames.user
        const {id, page, order_by, keyword, content, recommend, fid} = params
        switch (name) {
            // 浏览版面主题
            case threadRouteName.forumList:
                return new ForumListParam(id, page, order_by);
            // 搜索版面主题
            case threadRouteName.forumSearch:
                return new ForumSearchParam(id, keyword, page, content, recommend);
            // 浏览合集主题
            case threadRouteName.colList:
                return new colListParam(id, page, order_by);
            // 搜索合集主题
            case threadRouteName.colSearch:
                return new ColSearchParam(id, keyword, page, content, recommend);
            // 精华区
            case threadRouteName.recommend:
                return new ForumRecommendParam(id, page);
            // 收藏主题
            case threadRouteName.favor:
                return new FavorParam(id, page);
            // 用户主题
            case userRouteName.topic:
                return new UserSearchParam(id, fid, page, undefined, recommend);
            // 用户回复
            case userRouteName.replies:
                return new UserSearchParam(id, fid, page, 1, undefined);
        }
    }

    /**
     * 根据参数生成官方网址
     * @param name 路由名称
     * @param params 参数
     * @returns {string}
     */
    static buildOfficialUrl(name, params) {
        return UrlUtils.getUrlByParam(api, {...this.buildParams(name, params)});
    }

    /**
     * 根据路由生成官方网址
     * @param route 路由
     * @returns {string}
     */
    static buildOfficialUrlFromRoute(route) {
        return this.buildOfficialUrl(route.name, Object.assign({}, route.params, route.query));
    }

    /**
     * 根据官方网址生成本站的路由对象
     * @param url url
     */
    static buildRouteFromUrl(url) {
        NgaDomain
    }
}